package com.nforce.linremote;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class EditDialogFragment extends DialogFragment implements
		OnClickListener {

	public interface NoticeDialogListener {
		public void onDialogPositiveClick(String address);
	}

	NoticeDialogListener mListener;

	private EditText mAddress;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();

		View view = inflater.inflate(R.layout.edit_dialog_fragment, null);
		builder.setView(view);

		builder.setNegativeButton(android.R.string.cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
					}
				});
		builder.setPositiveButton(android.R.string.ok, this);

		mAddress = (EditText) view.findViewById(R.id.address);

		Bundle arg = getArguments();
		if (arg != null) {
			String addr = arg.getString(Constants.EXTRA_IP_ADDRESS);
			mAddress.setText(addr);
		}

		return builder.create();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (NoticeDialogListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement NoticeDialogListener");
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		mListener.onDialogPositiveClick(mAddress.getText().toString());
	}
}
